<template>
	<view>
		
		<!-- -->
		
		<view class="editaddress">
			<view class="content">
				<view class="row">
					<view class="nominal">收货人</view>
					<view class="input"><input placeholder="请输入收货人姓名" v-model="addressData.name" type="text" />
					</view>
				</view>
				<view class="row">
					<view class="nominal">手机号码</view>
					<view class="input"><input placeholder="请输入收货人手机号码" v-model="addressData.mobile" type="number"
							maxlength="11" /></view>
				</view>
				<view class="row">
					<view class="nominal">所在地区</view>
					<view class="input selectcity" @tap="openPicker">
						<input placeholder="请选择省市区" disabled type="text" v-model="address" />
						<uni-icons type="right" size="18" color="#999999" class="listFlexIcon"></uni-icons>
					</view>
				</view>
				<view class="row">
					<view class="nominal">详细地址</view>
					<view class="input"><textarea style="font-size: 28upx;" v-model="addressData.address"
							auto-height="true" placeholder="输入详细地址" v-if="show == false"></textarea></view>
				</view>
				<view class="row">
					<view class="nominal" style="color: 666;margin-top: 10upx;">设为默认地址</view>
					<view class="input switch">
						<switch :color="colors" style="transform:scale(0.8)" @change="switchChange"
							:checked="addressData.default == 1"></switch>
					</view>
				</view>
			</view>
			<view class="save">
				<view class="btn" :style="'background:' + colors" @tap="submit">保存地址</view>
			</view>

			<!-- 省市区选择 province city area初始省市区设置 show:是否显示  @sureSelectArea：确认事件 @hideShow：隐藏事件-->
			<s-address v-if="addressData.province" :province="addressData.province" :city="addressData.city" :area="addressData.district" :show="show" @changeClick="changeClick" @sureSelectArea="onsetCity"
				@hideShow="onhideShow"></s-address>

		</view>

	</view>
</template>

<script>
	var QQMapWX = require("@/common/qqmap-wx-jssdk.js");
	var qqmapsdk;
	export default {
		data() {
			return {
				colors: '#FA802A',
				show: false,
				address: '',
				addressData: {
					type:'add',
					name: '',
					mobile: '',
					address: '',
					lng: '',
					lat: '',
					province:'',
					city:'',
					district:'',
					street:'',
					default: 2
				},
				isShow: true,
				
				
				address_id:''
			};
		},
		onLoad(option) {
			this.addressData.type = option.type;
			if(option.type == 'add') {
				this.getLocation()
				uni.setNavigationBarTitle({
					title:'添加收货地址'
				})
			} else if(option.type == 'edit') {
				uni.setNavigationBarTitle({
					title:'编辑收货地址'
				})
				this.address_id = option.id;
				this.getAddressDetail(option.id)
			}
		},
		methods: {
			// 打开省市区选择器事件处理
			openPicker() {
				console.log('执行打开地址选择器')
				this.show = true
			},
			//选择器更改省市区事件
			changeClick(value, value2, value3){
			
				this.addressData.province = value;
				this.addressData.city = value2;
				this.addressData.district = value3;
				
			},
			// 关闭省市区选择器事件处理
			onhideShow() {
				this.show = false
				console.log('执行了关闭地址选择器')
			},
			//选中省市区
			onsetCity(e) {
				let data = e.detail.target.dataset;
				let address = data.province + data.city + data.area;
				this.show = false
				this.addressData.address = address
			},
			// 
			switchChange(e) {
				this.addressData.default = e.detail.value ? 1 : 2;

			},
			// 获取当前市区定位
			getLocation() {
				let that = this
				uni.getFuzzyLocation({
					type: 'wgs84',
					success(res) {
						//填入自己的key
						qqmapsdk = new QQMapWX({
								key: "JTEBZ-O6Z6W-YCQR6-OEV3F-LICME-EKBRK"
							}),
							that.addressData.lat = res.latitude;
						that.addressData.lng = res.longitude;
						//wx.getFuzzyLocation()方法获取的只是所在地区的经纬度，
						//获取所在地区的具体城市信息还需要进行逆地址解析
						qqmapsdk.reverseGeocoder({
							location: {
								latitude: that.addressData.lat,
								longitude: that.addressData.lng
							},
							success: function(res) {
								// that.address = res.result.address_component;
								let data = res.result.address_component
								that.addressData.province = data.province;
								that.addressData.city = data.city;
								that.addressData.district = data.district;
								that.addressData.street = data.street;
								that.address = data.province + data.city + data.district
								that.addressData.address = res.result.address
							},
							fail: function(err) {
								console.log(err);
							},
						})
					}
				})
			},
			async getAddressDetail(address_id) {
				let res = await this.$api.getAddressDetail({
					address_id: address_id || this.address_id
				});
				if (res.code == 1) {
					let data = res.data;
					this.addressData.name = data.name
					this.addressData.mobile = data.mobile
					this.addressData.address = data.address
					this.addressData.default = data.default
					this.addressData.province = data.province
					this.addressData.city = data.city
					this.addressData.district = data.district
					this.addressData.id = address_id || this.address_id
					this.addressData.lng = data.lng
					this.addressData.lat = data.lat
					this.address = data.province + data.city + data.district
				}
			},
			async submit() {
				let res = await this.$api.addAddress(this.addressData);
				if (res.code == 1) {
					this.$wanlshop.msg(this.addressData.type == 'add' ? '添加成功' : '保存成功')
					setTimeout(() => {
						uni.navigateBack()
					}, 1500);
				}
				
			},
		}
	};
</script>
<style>
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.save {
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120upx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.save view {
		display: flex;
	}

	.save .btn {
		box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.4);
		width: 70%;
		height: 80upx;
		border-radius: 80upx;
		background-color: #f23a3a;
		color: #fff;
		justify-content: center;
		align-items: center;
		font-size: 30upx;
		margin-bottom: 60px;
	}

	.save .btn .icon {
		height: 80upx;
		color: #fff;
		font-size: 30upx;
		justify-content: center;
		align-items: center;
	}

	.content {
		display: flex;
		flex-wrap: wrap;
		margin-top: 40upx;
	}

	.content view {
		display: flex;
	}

	.content .row {
		width: 92%;
		margin: 0 4%;
		border-bottom: solid 1upx #eee;
		display: flex;
		justify-content: flex-start;
		align-items: center;
	}
	.content .row:last-child {
		border: none;
	}
	.content .row .nominal {
		width: 30%;
		height: 80upx;
		font-size: 30upx;
		font-family: Droid Sans Fallback;
		font-weight: 400;
		color: rgba(51, 51, 51, 1);
		align-items: center;
	}

	.content .row .input {
		width: 70%;
		padding: 20upx 0;
		align-items: center;
		font-size: 30upx;

	}

	.content .row .input input {
		font-size: 28upx;
		color: #333333;
	}

	.content .row .switch {
		justify-content: flex-end;
	}

	.content .row .input textarea {
		min-height: 40upx;
		line-height: 40upx;
	}

	.content .del_box {
		width: 100%;
		display: block;
		overflow: hidden;
		margin-right: 30upx;
	}

	.content .del {
		width: 240upx;
		height: 80upx;
		float: right;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #F23A3A;
		margin: 0 auto;
		margin-top: 50upx;
		border-radius: 38upx;
		background-color: rgba(255, 0, 0, 0.05);
		border-bottom: solid 1upx #eee;
	}

	.selectcity input {
		width: 90%;
	}

	.selectcity image {
		width: 40upx;
		height: 40upx;
		float: right;
	}
</style>